﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Somnus.Objects
{
    class AnimatedObject : Objeto2D
    {
        private AnimationStructure animation;
        private List<AnimationStructure> animationList;

        public struct AnimationStructure
        {
            public int frameWidth;
            public int frameHeight;
            public int frameCount;
            public int framesPerSecond;
            public int X;
            public int Y;
            public string name;
        }

        public AnimatedObject(Texture2D textura, Nullable<Rectangle> sourceRectangle, float depth)
            : base(textura, sourceRectangle, depth)
        {
            if (textura != null)
            {
                animationList = new List<AnimationStructure>();

                animation = new AnimationStructure();
                animation.name = "default";
                animation.frameCount = 1;
                animation.X = 0;
                animation.Y = 0;
                animation.framesPerSecond = 2;
                if (sourceRectangle != null)
                {
                    Rectangle rectangle = (Rectangle)sourceRectangle;
                    animation.frameWidth = rectangle.Width;
                    animation.frameHeight = rectangle.Height;
                }
                else
                {
                    animation.frameWidth = textura.Width;
                    animation.frameHeight = textura.Height;
                }

                animationList.Add(animation);
            }
        }

        public override void draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (textura != null)
            {
                int frame = (int)(gameTime.TotalGameTime.TotalSeconds * animation.framesPerSecond) % animation.frameCount;

                sourceRectangle = new Rectangle(
                                animation.X + frame * animation.frameWidth,
                                animation.Y,
                                animation.frameWidth,
                                animation.frameHeight);

                base.draw(gameTime, spriteBatch);
            }
        }

        public String Animation
        {
            get
            {
                return animation.name;
            }
            set
            {
                for (int a = 0; a < animationList.Count; a++)
                {
                    if (animationList[a].name == value)
                    {
                        animation = animationList[a];
                    }
                }
            }
        }

        public void addAnimation(AnimationStructure animation)
        {
            animationList.Add(animation);
        }

        public String getAnimationNames()
        {
            String names = "";

            for (int a = 0; a < animationList.Count; a++)
            {
                names = animationList[a].name + " ";
            }

            return names;
        }

    }
}
